<?php
include("general_include.php");
include "checklogin.php";
include "top.php";
//include "left.php";
$user_ladder_id = $_REQUEST['user_ladder_id'];
$extra_content = "";

$lesson_id=$_REQUEST['lesson_id'];

if(isset($_POST['submit']))
{
   
	/*echo '<pre>';
	print_r($_REQUEST);
	  exit;*/
   $Formval = $_POST;
   $lesson_id  = $_POST['lesson_id'];
   $student_id = $_POST['student_id'];
   $coach_id = $_SESSION['user_id'];
   $lesson_price = $_POST['lesson_price'];
   $lesson_title = stripslashes($_POST['lesson_title']);
   $lesson_description = stripslashes($_POST['lesson_description']);
   $lesson_commision_admin = 20;
   $lesson_status_id = 1;
   $valid=1;
   
	   if(empty($lesson_id))
	   {
	
			   $Sql_ins= "INSERT INTO ".TABLEPREFIX."_lesson 
						  SET lesson_title = '".$lesson_title."',
							  lesson_description = '".$lesson_description."',
							  coach_id = '".$coach_id."',
							  student_id = '".$student_id."',
							  lesson_price = '".$lesson_price."',
							  lesson_commision_admin = '".$lesson_commision_admin."',
							  lesson_status_id = '".$lesson_status_id."',
							  date_added='".date("Y-m-d H:i:s")."'";
			  $UserManagerObjAjax->Execute($Sql_ins);
			  
  			   if(!mysql_error())
			   {
				 // header("location:my_game.php");
				  echo "<script>window.location.href='my_lesson.php'</script>";
			   }
				   
	   }
	   else if(!empty($lesson_id))
	   {
		   
		   /*$dupchk = "SELECT * FROM ".TABLEPREFIX."_user_game where game_id='$game_id' and user_id='".$_SESSION['user_id']."' and user_game_id!='$user_game_id'";
		   $chkArr = $UserManagerObjAjax->GetRecords('Row',$dupchk);
		   $Numrow = count($chkArr);
		   if($Numrow > 0)
		   {
			 $ermsg ='This game is already added';
			 $valid = 0;  
		   } 
		   if($valid ==1)
		   {*/
			   $Sql= "UPDATE ".TABLEPREFIX."_lesson 
					   SET lesson_title = '".$lesson_title."',
						   lesson_description = '".$lesson_description."',
						   student_id = '".$student_id."',
						   lesson_price = '".$lesson_price."'
					   WHERE lesson_id = '".$lesson_id."'";  
			   $UserManagerObjAjax->Execute($Sql);
			   /*$Sql= "UPDATE ".TABLEPREFIX."_user_game set game_id='$game_id',ladder_id='$ladder_id',server_id='$servertxt_id',race_id='$racetxt_id', 
			   region_id='$regiontxt_id', rating_id='$ratingtxt_id',experience='".addslashes($experience)."', rate='$ratevalue',
			   comment='".addslashes($comment)."',is_active='$is_active',date_edited='".date("Y-m-d")."' where user_game_id='".$_GET['user_game_id']."'";
			   $UserManagerObjAjax->Execute($Sql);*/
			   if(!mysql_error())
			   {
				  echo "<script>window.location.href='my_lesson.php'</script>";
			   }
		  // }	   
	   }	   

}

if(!empty($_REQUEST['lesson_id']))
{
	/* Get Record For Display Starts */	

	$SelectgameSql="SELECT * FROM ".TABLEPREFIX."_lesson WHERE lesson_id='".$_REQUEST['lesson_id']."'";
	$Formval = $UserManagerObjAjax->GetRecords("Row",$SelectgameSql);
    $SubmitButton="View";
	
	$Formval['lesson_title']		= htmlspecialchars(addslashes($Formval['lesson_title']));
	$Formval['lesson_price']		= htmlspecialchars(addslashes($Formval['lesson_price']));
	$Formval['lesson_description']	= htmlspecialchars(addslashes($Formval['lesson_description']));
	$Formval['coach_id']			= $Formval['coach_id'];
	
	$Sql_Coach="SELECT name FROM ".TABLEPREFIX."_user WHERE user_id=".$Formval['coach_id'];
	$Coach_Name = $UserManagerObjAjax->GetRecords("Row",$Sql_Coach);
	
	$Formval['coach_name']			= $Coach_Name['name'];
	$Formval['lesson_id']			= $Formval['lesson_id'];
	$Formval['lesson_status_id']	= $Formval['lesson_status_id'];
	
	$Sql_Status="SELECT lesson_status_title FROM ".TABLEPREFIX."_lesson_status WHERE lesson_status_id=".$Formval['lesson_status_id'];
	$Status_Title = $UserManagerObjAjax->GetRecords("Row",$Sql_Status);
	
	$Formval['lesson_status_title']	= $Status_Title['lesson_status_title'];
}
else
{
	$SubmitButton="Add";
}

$StudentSql = "select user.user_id,user.name FROM ".TABLEPREFIX."_user as user,".TABLEPREFIX."_user_type as type,".TABLEPREFIX."_user_type_user_relation as rel where rel.user_id=user.user_id and rel.user_type_id=type.user_type_id and type.user_type_id=2 and user.is_active='Y'";
$StudentArr = $UserManagerObjAjax->HtmlOptionArrayCreate($StudentSql);

$smarty->assign('is_coach',$is_coach);
$smarty->assign('is_partner',$is_partner);
$smarty->assign('extra_content',$extra_content);
$smarty->assign('Formval',$Formval);
$smarty->assign('ermsg',$ermsg);
$smarty->assign('Formval',$Formval);
$smarty->assign('SubmitButton',$SubmitButton);
$smarty->assign('StudentArr',$StudentArr);
$smarty->assign('user_game_id',$user_game_id);
$smarty->assign('lesson_id',$lesson_id);
$smarty->display("check_lesson_update.tpl");
include("footer.php");
?>